package com.candy.candylog.event;

import com.candy.candycommon.utils.ApplicationContentUtils;
import com.candy.candylog.business.entity.LogLink;
import com.candy.candylog.business.service.ILogLinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author suYanLong
 * @date 2022年10月14日 14:27
 */
@Slf4j
@Component
public class LogListener implements ApplicationListener<LogEvent> {

    @Resource
    ILogLinkService logLinkService;

    @Override
    public void onApplicationEvent(LogEvent logEvent) {

        if (logEvent instanceof LogEvent.LogLinkEvent) {
            log.info("请求链路日志事件");
            LogLink logLink = (LogLink) logEvent.getData();
            ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) ApplicationContentUtils.getApplicationContext().getBean("asyncTaskExecutor");
            executor.execute(() -> {
                logLinkService.saveOrUpdateData(logLink);
            });
        }
    }

//    @EventListener
//    public void scoreEvent(LogEvent scoreEvent) {
//        log.info(" =============>>>> ");
//        System.out.println(scoreEvent.getName());
//        System.out.println(scoreEvent.getScore());
//    }

}
